package com.example.vv.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;

import java.util.ArrayList;
import java.util.List;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

public abstract class BaseRecyclerViewAdapter<T> extends RecyclerView.Adapter<RecyclerViewHolder> implements View.OnClickListener {
    private Context context;
    private List<T> mData = new ArrayList<>();
    private int mLayoutId;
    private OnItemClickListener mListener;

    public BaseRecyclerViewAdapter(Context context, List<T> mData, int mLayoutId) {
        this.context = context;
        this.mData = mData;
        this.mLayoutId = mLayoutId;
    }
    @NonNull
    @Override
    public RecyclerViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(mLayoutId,parent,false);
        view.setOnClickListener(this);
        return new RecyclerViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerViewHolder holder, final int position) {
        holder.itemView.setTag(position);
        T bean = mData.get(position);
        onBindData(holder,bean,position);
    }

    @Override
    public int getItemCount() {
        return mData.size();
    }

    @Override
    public void onClick(View v) {
        if (mListener!=null) {
            mListener.onItemClick(this,v,(Integer) v.getTag());
        }
    }
    public void setOnItemClickListener(OnItemClickListener onItemClickListener){
        this.mListener = onItemClickListener;
    }

    protected abstract void onBindData(RecyclerViewHolder holder, T bean, int position);

    public interface OnItemClickListener{
        void onItemClick(RecyclerView.Adapter adapter, View v,int position);
    }
}
